package com.tsd.jxc.setting.controller;

import com.tsd.CommonParamNameConstants;
import com.tsd.common.controller.BaseController;
import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.jxc.setting.service.JxcSettingService;
import com.tsd.jxc.vo.JxcSettingVo;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserExt;
import com.tsd.system.service.SysUserService;
import io.swagger.annotations.Api;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * JxcSettingController
 *
 * @author Hillpool
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "进销存-配置", description = "配置的API描述")
public class JxcSettingController extends BaseController {

    @Resource
    private JxcSettingService jxcSettingService;
    @Resource
    private SysUserService sysUserService;

    @ResponseBody
    @RequestMapping(value = "/getJxcSetting.action", method = RequestMethod.POST)
    public HttpResult<Object> getDiningSetting() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            JxcSettingVo settingVo = jxcSettingService.getJxcSettingVo(DataSourceContextHolder.getDataSourceType());
            hr.setData(settingVo);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ResponseBody
    @RequestMapping(value = "/clearJxcSettingCache.action", method = RequestMethod.POST)
    public HttpResult<Object> clearDiningSettingCache() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            jxcSettingService.clearJxcSettingVoCache(DataSourceContextHolder.getDataSourceType());
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ResponseBody
    @RequestMapping(value = "/jxcDataInit.action", method = RequestMethod.POST)
    public HttpResult<Object> jxcDataInit() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(2);
            params.put(CommonParamNameConstants.LOGIN_ID, opUser.getLogin_id());
            params.put(CommonParamNameConstants.PASSWORD, map.get(CommonParamNameConstants.PASSWORD));
            SysUserExt user = sysUserService.loginUser(params);
            if (user == null) {
                return HttpResult.fail(ErrorCode.CODE_NO_LOGINNED, "用户信息不存在");
            }
            String type = HlpUtils.getString(map, "type");
            jxcSettingService.dataInit(type);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
